package com.yanfan.alarm.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.alarm.domain.IbmsAlarmRecord;
import com.yanfan.alarm.service.IIbmsAlarmRecordService;
import com.yanfan.alarm.service.impl.IbmsAlarmRecordServiceImpl;
import com.yanfan.common.core.domain.AjaxResult;
import net.sf.jsqlparser.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
@RequestMapping("/PushAlarm")
public class InTrusionController {
    @Autowired
    IIbmsAlarmRecordService ibmsAlarmRecordService;

    @PostMapping("/AlarmEvent")
    public AjaxResult getData(@RequestBody String data) throws UnsupportedEncodingException,
            ParseException {

        /**
         * param={"DeviceId":00001234,"AlarmTime":"2024-09-23 16:57:00","Chanel":1,"SubChanel":"","CID":"1301"}
         */
        System.out.println("获取报警信息中================================");
        System.out.println(data);
        String decode = URLDecoder.decode(data);
        System.out.println("获取完成================================");
        System.out.println();

        //转换成json格式
        String substring = decode.substring(6).replaceAll("\"","\"").trim();
        JSONObject jsonData = JSON.parseObject(substring);
        System.out.println("jsonData======"+jsonData);
        IbmsAlarmRecord ibmsAlarmRecord = new IbmsAlarmRecord();
        ibmsAlarmRecord.setAlarmCode(jsonData.get("CID").toString());
        ibmsAlarmRecord.setDeviceId(jsonData.get("DeviceId").toString());
        ibmsAlarmRecord.setChannel(jsonData.get("Channel").toString());
        ibmsAlarmRecord.setSubChannel(jsonData.get("SubChannel").toString());
        ibmsAlarmRecord.setAlarmTime(jsonData.get("AlarmTime").toString());
       return AjaxResult.success(ibmsAlarmRecordService.saveOrUpdate(ibmsAlarmRecord));
    }

    /**
     *1.5报警主机发送设备或通道的状态变化 (DeviceStatus)
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParseException
     */
    @PostMapping("/DeviceStatus")
    public AjaxResult getDeviceStatus(@RequestBody String data) throws UnsupportedEncodingException, ParseException {

        System.out.println();
        System.out.println("获取报警信息中================================");
        System.out.println(data);

        String decode = URLDecoder.decode(data);
        System.out.println(decode);


        System.out.println("获取完成================================");
        System.out.println();

        //转换成json格式
        String substring = decode.substring(6).replaceAll("\"","\"").trim();
        JSONObject jsonData = JSON.parseObject(substring);
        System.out.println("jsonData======"+jsonData);
        return AjaxResult.success();
    }

    /**
     * 1.6报警主机发送设备通道状态 (SectorStatus)
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParseException
     */

    @PostMapping("/SectorStatus")
    public AjaxResult getSectorStatus(@RequestBody String data) throws UnsupportedEncodingException, ParseException {

        System.out.println();
        System.out.println("获取报警信息中================================");
        System.out.println(data);

        String decode = URLDecoder.decode(data);
        System.out.println(decode);

        System.out.println("获取完成================================");
        System.out.println();

        //转换成json格式
        String substring = decode.substring(6).replaceAll("\"","\"").trim();
        JSONObject jsonData = JSON.parseObject(substring);
        System.out.println("jsonData======"+jsonData);

        return AjaxResult.success();
    }


}
